onesecond$Y=((onesecond$Y*0.0000625)*9.81)
onesecond$Z=((onesecond$Z*0.0000625)*9.81)
# Save or load binary version of converted data
save(onesecond,file="Onesecond.RData")
load("Onesecond.RData")
# Define times for time series
dt <- 1			# Time step in seconds = highest frequency present in the signal
N <- 256		# Number of samples
t <- seq(from=0, to = (N-1)*dt, by=dt)
# Extract a block of data
start <- 10000		# Define start time
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
line(t,x)
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black"))
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
xline
tes=data.frame(xline)
summary(xline)
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
head(dispX)
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
head(x)
head(onesecond)
onesecond["10000"]
onesecond[,10000]
onesecond[10000,2]
onesecond[10000,1]
# Define libraries required
library(matlab) 		# Provides access to Matlab-like basic functions
library(SynchWave)	# Provides fftshift and ifftshift functions
# Define subroutines
source("displacement.R")
###IMPORTING DATA
# 1 second data import
onesecond <- read.csv("Omokoroa one sec data 20150817.csv", header = T) # shortcut for csv data
onesecond$Time <- as.POSIXct(paste(onesecond$Date, onesecond$Time), format="%d/%m/%Y %H:%M:%S") # merging date and time together
onesecond <- onesecond[-c(1)] # removing the now un-needed date column
# Convert mA acceleration into m/s2
colnames(onesecond)=c("Time","X","Y","Z") # changing coloums names
onesecond$X=((onesecond$X*0.0000625)*9.81)
onesecond$Y=((onesecond$Y*0.0000625)*9.81)
onesecond$Z=((onesecond$Z*0.0000625)*9.81)
# Save or load binary version of converted data
save(onesecond,file="Onesecond.RData")
load("Onesecond.RData")
# Define times for time series
dt <- 1			# Time step in seconds = highest frequency present in the signal
N <- 256		# Number of samples
t <- seq(from=0, to = (N-1)*dt, by=dt) #time (period)
# Extract a block of data
start <- 1000		# Define start time "2015-08-10 14:17:49 NZST"
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
# Fit trend lines to data to allow data to be detrended
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
# Plot raw data
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
# Adjust data to a zero mean
#xadj <- x - mean(x)
#yadj <- y - mean(y)
# zadj <- z - mean(z)
# Adjust data to zero mean with no trend (makes the data have a mean of 0, keeping it around 0, no trend, just straight up residuals)
xadj <- xline$residuals #just extracting residuals, other in data include call/coefficients/fitted values
yadj <- yline$residuals
zadj <- zline$residuals
# Plot adjusted data
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
# Plot displacements
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
# Extract a block of data
start <- 20000		# Define start time "2015-08-10 14:17:49 NZST"
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
# Fit trend lines to data to allow data to be detrended
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
# Plot raw data
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
# Adjust data to a zero mean
#xadj <- x - mean(x)
#yadj <- y - mean(y)
# zadj <- z - mean(z)
# Adjust data to zero mean with no trend (makes the data have a mean of 0, keeping it around 0, no trend, just straight up residuals)
xadj <- xline$residuals #just extracting residuals, other in data include call/coefficients/fitted values
yadj <- yline$residuals
zadj <- zline$residuals
# Plot adjusted data
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
# Plot displacements
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
# Extract a block of data
start <- 2000		# Define start time "2015-08-10 14:17:49 NZST"
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
# Fit trend lines to data to allow data to be detrended
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
# Plot raw data
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
# Adjust data to a zero mean
#xadj <- x - mean(x)
#yadj <- y - mean(y)
# zadj <- z - mean(z)
# Adjust data to zero mean with no trend (makes the data have a mean of 0, keeping it around 0, no trend, just straight up residuals)
xadj <- xline$residuals #just extracting residuals, other in data include call/coefficients/fitted values
yadj <- yline$residuals
zadj <- zline$residuals
# Plot adjusted data
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
# Plot displacements
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
dt <- 1			# Time step in seconds = highest frequency present in the signal
N <- 512		# Number of samples
t <- seq(from=0, to = (N-1)*dt, by=dt) #time (period)
# Extract a block of data
start <- 10000		# Define start time "2015-08-10 14:17:49 NZST"
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
# Fit trend lines to data to allow data to be detrended
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
# Plot raw data
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
# Adjust data to a zero mean
#xadj <- x - mean(x)
#yadj <- y - mean(y)
# zadj <- z - mean(z)
# Adjust data to zero mean with no trend (makes the data have a mean of 0, keeping it around 0, no trend, just straight up residuals)
xadj <- xline$residuals #just extracting residuals, other in data include call/coefficients/fitted values
yadj <- yline$residuals
zadj <- zline$residuals
# Plot adjusted data
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
# Plot displacements
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
dt <- 1			# Time step in seconds = highest frequency present in the signal
N <- 1024		# Number of samples
t <- seq(from=0, to = (N-1)*dt, by=dt) #time (period)
# Extract a block of data
start <- 10000		# Define start time "2015-08-10 14:17:49 NZST"
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
# Fit trend lines to data to allow data to be detrended
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
# Plot raw data
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
# Adjust data to a zero mean
#xadj <- x - mean(x)
#yadj <- y - mean(y)
# zadj <- z - mean(z)
# Adjust data to zero mean with no trend (makes the data have a mean of 0, keeping it around 0, no trend, just straight up residuals)
xadj <- xline$residuals #just extracting residuals, other in data include call/coefficients/fitted values
yadj <- yline$residuals
zadj <- zline$residuals
# Plot adjusted data
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
# Plot displacements
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
dt <- 1			# Time step in seconds = highest frequency present in the signal
N <- 128		# Number of samples
t <- seq(from=0, to = (N-1)*dt, by=dt) #time (period)
# Extract a block of data
start <- 10000		# Define start time "2015-08-10 14:17:49 NZST"
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
# Fit trend lines to data to allow data to be detrended
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
# Plot raw data
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
# Adjust data to a zero mean
#xadj <- x - mean(x)
#yadj <- y - mean(y)
# zadj <- z - mean(z)
# Adjust data to zero mean with no trend (makes the data have a mean of 0, keeping it around 0, no trend, just straight up residuals)
xadj <- xline$residuals #just extracting residuals, other in data include call/coefficients/fitted values
yadj <- yline$residuals
zadj <- zline$residuals
# Plot adjusted data
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
# Plot displacements
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
dt <- 1			# Time step in seconds = highest frequency present in the signal
N <- 256		# Number of samples
t <- seq(from=0, to = (N-1)*dt, by=dt) #time (period)
# Extract a block of data
start <- 10000		# Define start time "2015-08-10 14:17:49 NZST"
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
# Fit trend lines to data to allow data to be detrended
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
# Plot raw data
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
# Adjust data to a zero mean
#xadj <- x - mean(x)
#yadj <- y - mean(y)
# zadj <- z - mean(z)
# Adjust data to zero mean with no trend (makes the data have a mean of 0, keeping it around 0, no trend, just straight up residuals)
xadj <- xline$residuals #just extracting residuals, other in data include call/coefficients/fitted values
yadj <- yline$residuals
zadj <- zline$residuals
# Plot adjusted data
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
# Plot displacements
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
# Define libraries required
library(matlab) 		# Provides access to Matlab-like basic functions
library(SynchWave)		# Provides fftshift and ifftshift functions
library(signal)			# Provides signal processing functions
dt = 0.01
N = 512
t = seq(from=0, to = (N-1)*dt, by=dt)
wave_freq=1
acc_time_data = sin(2*pi*wave_freq*t)
source("displacement.R")
disp = displacement(acc_time_data,dt)
analytical = -1/(2*pi*wave_freq)^2*sin(2*pi*wave_freq*t)
bf = butter(3,0.0008,type="high")
dispf = filter(bf,disp)
plot(t,disp,type="l",col="grey")
lines(t,analytical,col="blue",lty=2)
lines(t,dispf,col="red",lty=1)
# Routine to estimate the displacement from a time series of accelerations
# by performing double integration in frequency space
#  Note this function requires the packages SynchWave and Matlab
displacement = function (accel,dt) {
nn <- length(accel)			# Length of time series
n <- nextn(nn,factors=2)	# Determine next largest power of 2   #nextn=the smallest interger greater than or equal to nn. facotr=the power to use, then nextn find the next largest power
if (n>nn) {					# Pad with zeros to next power of 2 if necessary
accel[nn+1:n] <- 0
}
df <- 1/(n*dt)				# Frequency increment (bandwidth)
Nyq <- 1/(2*dt)				# Nydquist frequency = the minimum rate at which a signal can be sampled without introducing errors, which is twice the highest frequency present in the signal.
acc_freq_data <- fftshift(fft(accel,inverse= FALSE))	# Undertake fourier analysis and swap left & right halves of the result
f <- seq(from=-Nyq, to=Nyq-df, by=df)					# Define corresponding frequencies
disp_freq_data <- zeros(size(acc_freq_data))			# Create an array for the results
divisor <- (2*pi*f*1i)^2								# Calulate the omega divisor for double integegration
ix <- which(divisor != 0)								# Determine non-zero divisors to avoid division by zero problems
disp_freq_data[ix] <- acc_freq_data[ix]/divisor[ix]		# Double integrate
disp_freq_data <- as.vector(disp_freq_data)				# Convert to vector
disp_time_data <- fft(ifftshift(disp_freq_data),inverse=TRUE)/n		# Inverse fourier transform
displacement <- Re(disp_time_data[1:nn])	# Extract real component of displacement time series of original length (adjust for padding if it occurred)
}
# Define libraries required
library(matlab) 		# Provides access to Matlab-like basic functions
library(SynchWave)	# Provides fftshift and ifftshift functions
# Define subroutines
source("displacement.R")
###IMPORTING DATA
# 1 second data import
onesecond <- read.csv("Omokoroa one sec data 20150817.csv", header = T) # shortcut for csv data
onesecond$Time <- as.POSIXct(paste(onesecond$Date, onesecond$Time), format="%d/%m/%Y %H:%M:%S") # merging date and time together
onesecond <- onesecond[-c(1)] # removing the now un-needed date column
# Convert mA acceleration into m/s2
colnames(onesecond)=c("Time","X","Y","Z") # changing coloums names
onesecond$X=((onesecond$X*0.0000625)*9.81)
onesecond$Y=((onesecond$Y*0.0000625)*9.81)
onesecond$Z=((onesecond$Z*0.0000625)*9.81)
# Save or load binary version of converted data
save(onesecond,file="Onesecond.RData")
load("Onesecond.RData")
# Define times for time series
dt <- 1			# Time step in seconds = highest frequency present in the signal
N <- 256		# Number of samples
t <- seq(from=0, to = (N-1)*dt, by=dt) #time (period)
# Extract a block of data
start <- 10000		# Define start time "2015-08-10 14:17:49 NZST"
x <- onesecond$X[start:(start+N-1)]
y <- onesecond$Y[start:(start+N-1)]
z <- onesecond$Z[start:(start+N-1)]
# Fit trend lines to data to allow data to be detrended
xline <- line(t,x)
yline <- line(t,y)
zline <- line(t,z)
# Plot raw data
op <- par(mfcol=c(3,1))
plot(t,x,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="X Axis")
abline(coef(xline,col="black")) #trend (the coeficient of x over time)
plot(t,y,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="Y Axis")
abline(coef(yline,col="black"))
plot(t,z,type="l",col="blue",xlab="Time(s)",ylab="Acceleration (m/s2)",main="z Axis")
abline(coef(zline,col="black"))
par(op)
# Adjust data to a zero mean
#xadj <- x - mean(x)
#yadj <- y - mean(y)
# zadj <- z - mean(z)
# Adjust data to zero mean with no trend (makes the data have a mean of 0, keeping it around 0, no trend, just straight up residuals)
xadj <- xline$residuals #just extracting residuals, other in data include call/coefficients/fitted values
yadj <- yline$residuals
zadj <- zline$residuals
# Plot adjusted data
dev.new()
op <- par(mfcol=c(3,1))
plot(t,xadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="X axis")
plot(t,yadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Y axis")
plot(t,zadj,type="l",col="blue",xlab="Time (s)",ylab="Acceleration (m/s2)",main="Z axis")
par(op)
# Convert to displacements
dispX = displacement(xadj,dt)
dispY = displacement(yadj,dt)
dispZ = displacement(zadj,dt)
# Plot displacements
dev.new()
op <- par(mfcol=c(3,1))
plot(t,dispX,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="X Axis")
plot(t,dispY,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Y Axis")
plot(t,dispZ,type="l",col="blue",xlab="Time (s)",ylab="Displacement (m)",main="Z Axis")
par(op)
